// pages/record/record.js
const request = require('../../utils/request.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    screenHeight: "",
    isSelected: 0,
    date: "",
    requestData: {
      pageNum: 1,
      pageSize: 10,
      keywords: "",
      time: Date,
    },
    list: "",
    totalPages: "",
    info: "",
    isShow: false,
  },
  changeIsSelected: function (e) {
    this.setData({
      isSelected: e.target.dataset.index
    })
  },
  bindDateChange: function (e) {
    console.log(e.detail.value)
    var nowDate = e.detail.value
    var that = this
    this.setData({
      date: nowDate.substring(0, 4) + '年' + nowDate.substring(5, 7) + '月' + nowDate.substring(8, 10) + '日'
    })
    this.data.requestData.time = nowDate.substring(0, 4) + '/' + nowDate.substring(5, 7) + '/' + nowDate.substring(8, 10);
    console.log(this.data.requestData);
    this.loadData();
  },
  loadData: function (e) {
    var that = this;
    request.healthRecords(this.data.requestData, this.data.info.token).then(res => {
      if (!res.data.data.content[0]) {
        console.log("不存在");
        that.setData({
          isShow: true,
        })
      } else {
        for (let i in res.data.data.content) {
          var date = new Date(res.data.data.content[i].time);
          res.data.data.content[i].time = that.formatDate(date);
          console.log(res.data.data.content[i].time);
        }
        that.setData({
          isShow: false,
        })
      }
      that.setData({
        list: res.data.data.content,
        totalPages: res.data.data.totalPages,
      })

    })
  },
  getMoreTen: function (e) {
    var that = this;
    if (this.data.totalPages == this.data.requestData.pageNum) {
      wx.showToast({
        title: '无更多数据',
        icon: 'none',
      })
    } else {
      that.data.requestData.pageNum = that.data.requestData.pageNum + 1;
      that.loadData();
    }
  },
  //格式化xx年xx月xx日
  formatDate: function (d) {
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month) : curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date) : curr_date;
    var yyyyMMdd = curr_year + "年" + curr_month + "月" + curr_date + "日";
    return yyyyMMdd;
  },
  //格式化xx/xx/xx
  format_Date: function (d) {
    var curr_date = d.getDate();
    var curr_month = d.getMonth() + 1;
    var curr_year = d.getFullYear();
    String(curr_month).length < 2 ? (curr_month = "0" + curr_month) : curr_month;
    String(curr_date).length < 2 ? (curr_date = "0" + curr_date) : curr_date;
    var yyyyMMdd = curr_year + "/" + curr_month + "/" + curr_date;
    return yyyyMMdd;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 显示本地的屏幕高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.screenHeight - 44 - 57 - res.statusBarHeight
        })
      },
    })
    //token
    var info = wx.getStorageSync('userInfo');
    var date = new Date();
    //展示日期
    var newDate = this.formatDate(date);
    console.log(newDate);
    this.setData({
      date: newDate,
      info: info,
    })
    //请求日期
    this.data.requestData.time = this.format_Date(date);
    request.healthRecords(this.data.requestData, info.token).then(res => {
      for (let i in res.data.data.content) {
        var date = new Date(res.data.data.content[i].time);
        res.data.data.content[i].time = that.formatDate(date);
        console.log(res.data.data.content[i].time);
      }
      that.setData({
        list: res.data.data.content,
        totalPages: res.data.data.totalPages,
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})